'use strict';

/**
 * @ngdoc function
 * @name jgappApp.controller:AdministratorAdminBzCtrl
 * @description
 * # AdministratorAdminBzCtrl
 * Controller of the jgappApp
 */
angular.module('jgappApp')
    .controller('AdministratorAdminBzCtrl', function ($scope, $rootScope, openApi, Util, $location, $routeParams, FormSubmit) {
        var id = $scope.process_id = $routeParams.id
        var selectData, params = {};
        // 如果有回显的数据就用回显的数据
        // 获取选择好的数据
        $scope.param = params;
        // --------------------------------------
        // 选择报销单明细
        $scope.showCertificate = function () {
            var param = {};
            param.type = "证件类别";
            Util.getKeyValue(param, function (data) {
                $scope.resiTypeList = data.data
                $rootScope.$emit('showmasker');
                $scope.showCerPicker = true;
            })
        };
        $scope.deletedItem = function (index) {
            $scope.param.dealDetails.splice(index, 1);
            $scope.index = -1;
        };
        $scope.addCertificate = function () {
            $scope.picker = $scope.picker || {};
            var check = false;
            check = checkPicker($scope.picker);
            if (!check) {
                return;
            };
            $scope.param.dealDetails.push($scope.picker);
            // 隐藏表单
            hideCertificate();
        };
        var hideCertificate = function () {
            $scope.picker = {};
            $rootScope.$emit('closemasker');
            $scope.showCerPicker = false;
        };
        $scope.$on('closemasker', function () {
            hideCertificate()
        });
        $scope.cacelCertificate = function () {
            hideCertificate();
        };
        // 验证是否输入完整
        var checkPicker = function (data) {
            var check = true;
            if (!data.cerType) {
                Util.Toast("请选择办证类型");
                check = false;
                return check;
            }
            if (!data.personnels) {
                Util.Toast("请填写人员姓名");
                check = false;
                return check;
            }
            return check;

        };
        $scope.submit = function (operationId) {

            $scope.param.dealList = angular.toJson($scope.param.dealDetails);
            // 底部类型end
            var options = {};
            // 流程id
            options.id = id;
            // 根据operationId判断是草稿还是提交
            options.operationId = operationId;
            options.urlParam = openApi.certificateDeal;
            options.param = angular.copy($scope.param, {});
            delete options.param.dealDetails
            FormSubmit.submit(options,$scope);
        };
        $scope.swipe = function (index) {
            $scope.index = index;
        }
        $scope.resetlist = function (index) {
            $scope.index = -1;
        }
        $scope.validate = function () {
            if ($scope.param.dealDetails.length < 1) {
                Util.Toast("请添加办证明细");
                return;
            }
            if (!$scope.validation.passvalidation) {
                Util.Toast($scope.validation.tip);
                return;
            }
            return true;
        }
        var init = function () {
            // 获取回显的详细信息
            Util.getDetailData(id, function (data) {
                $scope.param = data;
                // 获取选择页面返回的数据并且合并到参数中
                // 用来显示选择的报销明细
                $scope.param.dealDetails = $scope.param.dealDetails || [];

            });
        };
        // 初始化
        init();

    });
